public class Solution {
    public static boolean wordPattern(String pattern, String str) {
        Map<String, String> map = new HashMap<>();
        Set<String> set = new HashSet<>();
        String[] pa = pattern.split("");
        String[] st = str.split(" ");
        if(pa.length != st.length || pattern.equals("")) {
            return false;
        }
        for(int i = 0; i < pa.length; i++) {
            if(map.containsKey(pa[i])) {
                if(!map.get(pa[i]).equals(st[i])) {
                    return false;
                }
            } else {
                if(set.contains(st[i]))
                    return false;
                map.put(pa[i], st[i]);
                set.add(st[i]);
            }

        }
        return true;
    }
}